/*!
 * \file dubbo_core.c
 *
 * \brief 共通定义
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 * 共通使用的宏和结构体
 *
 * \version 1.0
 * \author jikw
 * \date 2016/12/10
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "config.h"
#include "frsutl.h"
#include "frhash2.h"
#include "frct_comm.h"
#include "frservice_comm.h"
#include "frct_detect_hash.h"
#include "frvar_field.h"
#include "dubbo_serialize_hessian2.h"
#include "dubbo_serialize_dubbo.h"

extern void *g_p_apm_context;
struct timeval g_st_time;

/*!
 * dubbo私有数据创建
 *
 * \param void
 *
 * \return NULL          内存分配失败\n
 *         p_st_private  内存分配成功
 *
 * \note
 *
 */
FR_STATIC L7session_dubbo_private* __dubbo_private_create(void)
{
    L7session_dubbo_private *p_st_private = NULL;
    p_st_private = (L7session_dubbo_private*)frmalloc(sizeof(L7session_dubbo_private));
    if (p_st_private == NULL)
        return NULL;
    memset(p_st_private, 0x00, sizeof(L7session_dubbo_private));

    return p_st_private;
}

/*!
 * 判断是否是dubbo协议
 *
 * \param p_st_conn       (输入数据信息)
 * \param p_st_packet     (输入数据)
 *
 * \return APM_L7_IS_YES  属于dubbo协议\n
 *         APM_L7_IS_NO   不属于dubbo协议
 *
 * \note
 *
 */
int dubbo_detect(Conntrack *p_st_conn, Apm_packet *p_st_packet)
{
    int i_ret = 0;
    int *p_i_tmp;
    char arr_len[4];

    if (p_st_conn==NULL || p_st_packet == NULL ||
        p_st_packet->p_payload == NULL ||p_st_packet->ui_payload_len < 16)
        return APM_L7_IS_NO;

    memset(arr_len, 0x00, sizeof(arr_len));

    //协议格式<header><bodydata>
    //协议头<header>是16个字节的定长数据
    //2bytes short型的MAGIC = 0xdabb
    //1byte  一个字节的消息标志位
    //1byte  状态位，消息类型为response时，设置请求响应状态
    //8bytes long型的消息id
    //4bytes int型的消息体body长度

    if (memcmp(p_st_packet->p_payload, MAGIC, MAGIC_LENGTH) != 0)
        goto ERROR;

    if (((*(p_st_packet->p_payload + MAGIC_LENGTH)) & FLAG_REQUEST) == 0)
        goto ERROR;

    memcpy(arr_len, p_st_packet->p_payload+12, 4);
    p_i_tmp = (int *)arr_len;
    if (ntohl(*p_i_tmp) != p_st_packet->ui_payload_len-16)
        goto ERROR;

    i_ret = frct_get_direct_known(p_st_conn);
    if (i_ret == FR_DIRECT_UNKNOWN)
        frcap_set_cs_srv(p_st_conn, p_st_packet->dst_ip, p_st_packet->dst_port);
    frservice_add(g_p_apm_context, MODULE_L7PROTO, p_st_packet->dst_ip, p_st_packet->dst_port, NULL, 0);

    return APM_L7_IS_YES;

ERROR:
    if (frct_get_direct_known(p_st_conn) == FR_DIRECT_UNKNOWN)
    {
        i_ret = frservice_get(g_p_apm_context, MODULE_L7PROTO, p_st_packet->src_ip, p_st_packet->src_port, NULL, 0, NULL);
        if (i_ret == FR_SUCC)
        {
            frcap_set_cs_srv(p_st_conn, p_st_packet->src_ip, p_st_packet->src_port);
            return APM_L7_IS_YES;
        }
        i_ret = frservice_get(g_p_apm_context, MODULE_L7PROTO, p_st_packet->dst_ip, p_st_packet->dst_port, NULL, 0, NULL);
        if (i_ret == FR_SUCC)
        {
            frcap_set_cs_srv(p_st_conn, p_st_packet->dst_ip, p_st_packet->dst_port);
            return APM_L7_IS_YES;
        }
    }
    else
    {
        i_ret = frservice_get(g_p_apm_context, MODULE_L7PROTO, p_st_packet->src_ip, p_st_packet->src_port, NULL, 0, NULL);
        if (i_ret == FR_SUCC)
            return APM_L7_IS_YES;

        i_ret = frservice_get(g_p_apm_context, MODULE_L7PROTO, p_st_packet->dst_ip, p_st_packet->dst_port, NULL, 0, NULL);
        if (i_ret == FR_SUCC)
            return APM_L7_IS_YES;
    }

    return APM_L7_IS_NO;
}

/*!
 * 链表节点释放规则
 *
 * \Frlist_node *p_st_node
 *
 * \return void
 *
 * \note
 *
 */
FR_STATIC void __list_node_free(Frlist_node *p_st_node)
{
    dubbo_report_list *p_st_info = NULL;

    if (p_st_node==NULL || p_st_node->data == NULL )
        return;

    p_st_info = (dubbo_report_list *)FRLIST_GET_NODE_DATA(p_st_node);

    if (p_st_info->p_lst_req)
        frreport_body_l7list_free(&p_st_info->p_lst_req);
    if (p_st_info->p_lst_comm)
        frreport_body_l7list_free(&p_st_info->p_lst_comm);

    frfree(p_st_info);
    return;
}

/*!
 * req的p_lst_comm、p_lst_req、starttime和ID存进链表
 *
 * \param p_st_private    (dubbo私有数据)
 *
 * \return FR_FAIL        失败\n
 *         FR_SUCC        成功
 *
 * \note
 *
 */
FR_STATIC int __dubbo_add_req_list(L7session_dubbo_private *p_st_private)
{
    dubbo_report_list *p_st_report = NULL;
	Frlist_node *p_st_node = NULL;
	Frlist_node *p_st_gotnode = NULL;
	L4_transaction *p_st_l4_trans = NULL;

    if (p_st_private == NULL)
        return FR_FAIL;

    //创建链表
    if (p_st_private->p_report_list_dubbo == NULL)
        p_st_private->p_report_list_dubbo = frhash2_create2(100);

    p_st_l4_trans = &(p_st_private->st_l4_trans);

    //把req的p_lst_comm、p_lst_req和ID和时间存进链表
    p_st_report = (dubbo_report_list *)frmalloc(sizeof(dubbo_report_list));
    memset(p_st_report, 0x00, sizeof(dubbo_report_list));

    p_st_report->st_request_time.tv_sec = p_st_l4_trans->st_request_time.tv_sec;
    p_st_report->st_request_time.tv_usec = p_st_l4_trans->st_request_time.tv_usec;

    //拷贝相关链表
    p_st_report->p_lst_comm = FRREPORT_BODY_L7LIST_CLONE_COMM(p_st_private->p_st_body_data_req);
    if (p_st_report->p_lst_comm == NULL)
        goto ERROR;
    p_st_report->p_lst_req = FRREPORT_BODY_L7LIST_CLONE_REQ(p_st_private->p_st_body_data_req);
    if (p_st_report->p_lst_req == NULL)
        goto ERROR;

    p_st_node = frlist_node_create(p_st_report);
    //存list之前 判断key值是否存在 如存在删除节点 再加节点
    p_st_gotnode = frhash2_getLong(p_st_private->p_report_list_dubbo, p_st_private->u64_req_id);
    if (p_st_gotnode != NULL)
    {
        frhash2_del(p_st_private->p_report_list_dubbo, p_st_gotnode, __list_node_free);
        p_st_gotnode = NULL;
    }
    frhash2_addLong(p_st_private->p_report_list_dubbo, p_st_private->u64_req_id, p_st_node);

    return FR_SUCC;

ERROR:
    if (p_st_report)
        frfree(p_st_report);
    if (p_st_report->p_lst_comm)
        frreport_body_l7list_free(&p_st_report->p_lst_comm);
    if (p_st_report->p_lst_req)
        frreport_body_l7list_free(&p_st_report->p_lst_req);
    return APM_L7_ANALYZE_FAIL;
}

/*!
 * req header数据解析
 *
 * \param p_packet        (req数据)
 * \param p_st_private    (私有数据)
 * \param p_st_body_data
 *
 * \return APM_L7_ANALYZE_SUCC   成功\n
 *         APM_L7_ANALYZE_FAIL   失败
 *
 * \note
 *
 */
FR_STATIC int __req_header_parse(Frbyte_Packet *p_packet
                        , L7session_dubbo_private *p_st_private
                        , Frreport_body_data *p_st_body_data)
{
    uint32_t u32_len = 0;
    uint64_t u64_len = 0;
    char arr_temp[2] = {0};
    char arr_flag[2] = {0};
    char arr_name[32] = {0};

    if (p_packet == NULL || p_st_private == NULL || p_st_body_data == NULL)
        return APM_L7_ANALYZE_FAIL;

    memset(arr_temp, 0x00, sizeof(arr_temp));
    memset(arr_flag, 0x00, sizeof(arr_flag));
    memset(arr_name, 0x00, sizeof(arr_name));

    //取header
    //取2位MAGIC=(short)0xdabb
    if (bytebuff_get_bytes(p_packet, arr_temp, 2) == FR_FAIL)
        return APM_L7_ANALYZE_FAIL;
    if (memcmp(arr_temp, MAGIC, MAGIC_LENGTH) != 0)
        return APM_L7_ANALYZE_FAIL;

    //取1位消息标志位
    if (bytebuff_get_bytes(p_packet, arr_flag, 1) == FR_FAIL)
        return APM_L7_ANALYZE_FAIL;
    //不是请求返回
    if ((*arr_flag & FLAG_REQUEST) == 0)
        return APM_L7_ANALYZE_FAIL;
    //心跳返回
    if ((*arr_flag & FLAG_EVENT) != 0)
        return APM_L7_ANALYZE_FAIL;
    //twoway需要返回 oneway不需要返回
    if ((*arr_flag & FLAG_TWOWAY) == 0)
    {
        if (frreport_body_l7_add_int(p_st_body_data, APM_REPORT_KEY_REQ_ISRESPREQUIRED, 0) == FR_FAIL)
            return APM_L7_ANALYZE_FAIL;
        p_st_private->i_flag_oneway = 1;
    }
    else
    {
        if (frreport_body_l7_add_int(p_st_body_data, APM_REPORT_KEY_REQ_ISRESPREQUIRED, 1) == FR_FAIL)
            return APM_L7_ANALYZE_FAIL;
        p_st_private->i_flag_oneway = 0;
    }

    p_st_private->i_flag_serialize = *arr_flag & 0x1f;

    //跳过1位状态位
    if (bytebuff_skip(p_packet, 1) != FR_SUCC)
        return APM_L7_ANALYZE_FAIL;

    //取8位消息id
    if (bytebuff_get_long(p_packet, 8, &u64_len, BYTE_BIG_ENDIAN) == FR_FAIL)
        return APM_L7_ANALYZE_FAIL;
    p_st_private->u64_req_id = u64_len;

    //取4位body长度
    if (bytebuff_get_int(p_packet, 4, &u32_len, BYTE_BIG_ENDIAN) == FR_FAIL)
        return APM_L7_ANALYZE_FAIL;
    if (u32_len+16 < p_packet->ui_all_len)
        return APM_L7_ANALYZE_FAIL;

    return APM_L7_ANALYZE_SUCC;
}

/*!
 * resp header数据解析
 *
 * \param p_packet        (resp数据)
 * \param p_st_private    (私有数据)
 * \param p_st_body_data
 *
 * \return APM_L7_ANALYZE_SUCC   成功\n
 *         APM_L7_ANALYZE_FAIL   失败
 *
 * \note
 *
 */
FR_STATIC int __resp_header_parse(Frbyte_Packet *p_packet
                        , L7session_dubbo_private *p_st_private
                        , Frreport_body_data *p_st_body_data)
{
    uint8_t u8_len = 0;
    uint32_t u32_len = 0;
    uint64_t u64_len = 0;
    char arr_temp[2] = {0};
    char arr_flag[2] = {0};
    char arr_name[32] = {0};

    if (p_packet == NULL || p_st_private == NULL || p_st_body_data == NULL)
        return APM_L7_ANALYZE_FAIL;

    memset(arr_temp, 0x00, sizeof(arr_temp));
    memset(arr_flag, 0x00, sizeof(arr_flag));
    memset(arr_name, 0x00, sizeof(arr_name));

    //取2位MAGIC=(short)0xdabb
    if (bytebuff_get_bytes(p_packet, arr_temp, 2) == FR_FAIL)
        return APM_L7_ANALYZE_FAIL;
    if (memcmp(arr_temp, MAGIC, MAGIC_LENGTH) != 0)
        return APM_L7_ANALYZE_FAIL;

    //取1位消息标志位
    if (bytebuff_get_bytes(p_packet, arr_flag, 1) == FR_FAIL)
        return APM_L7_ANALYZE_FAIL;
    //不是响应返回
    if ((*arr_flag & FLAG_REQUEST) != 0)
        return APM_L7_ANALYZE_FAIL;
    //心跳返回
    if ((*arr_flag & FLAG_EVENT) != 0)
        return APM_L7_ANALYZE_FAIL;

    p_st_private->i_flag_serialize = *arr_flag & 0x1f;

    //取1位状态位
    if (bytebuff_get_char(p_packet, &u8_len) == FR_FAIL)
        return APM_L7_ANALYZE_FAIL;
    if (u8_len == 0x14)
        p_st_private->i_flag_status = 1;
    else
        p_st_private->i_flag_status = 0;

    //取8位消息id
    if (bytebuff_get_long(p_packet, 8, &u64_len, BYTE_BIG_ENDIAN) == FR_FAIL)
        return APM_L7_ANALYZE_FAIL;
    p_st_private->u64_resp_id = u64_len;

    //取4位body长度
    if (bytebuff_get_int(p_packet, 4, &u32_len, BYTE_BIG_ENDIAN) == FR_FAIL)
        return APM_L7_ANALYZE_FAIL;
    if (u32_len+16 < p_packet->ui_all_len)
        return APM_L7_ANALYZE_FAIL;

    return APM_L7_ANALYZE_SUCC;
}

/*!
 * req body数据解析
 *
 * \param p_packet        (resp数据)
 * \param p_st_private    (私有数据)
 * \param p_st_body_data
 *
 * \return APM_L7_ANALYZE_SUCC   成功\n
 *         APM_L7_ANALYZE_FAIL   失败
 *
 * \note
 *
 */
FR_STATIC int __req_body_parse(Frbyte_Packet *p_packet
                        , L7session_dubbo_private *p_st_private
                        , Frreport_body_data *p_st_body_data)
{
    int i_ret = 0;
    char *p_value = NULL;

    if (p_packet == NULL || p_st_private == NULL || p_st_body_data == NULL)
        return APM_L7_ANALYZE_FAIL;

    switch(p_st_private->i_flag_serialize)
    {
        case 1://dubbo序列化
            i_ret = serialize_dubbo_req_body_parse(p_packet, p_st_body_data);
            if (i_ret == APM_L7_ANALYZE_FAIL)
                return i_ret;
            break;

        case 2://hessian2序列化
            i_ret = serialize_hessian2_req_body_parse(p_packet, p_st_private, p_st_body_data);
            if (i_ret == APM_L7_ANALYZE_FAIL)
                return i_ret;
            break;

        case 5://json序列化
            p_value = (char *)frmalloc(p_packet->ui_all_len-16+1);
            if (p_value == NULL)
                return APM_L7_ANALYZE_FAIL;
            memset(p_value, 0x00, p_packet->ui_all_len-16+1);
            if (bytebuff_get_bytes(p_packet, p_value, p_packet->ui_all_len-16) == FR_FAIL)
            {
                frfree(p_value);
                return APM_L7_ANALYZE_FAIL;
            }
            if (frreport_body_l7_add_str(p_st_body_data, "request.rpc.arg", p_value) == FR_FAIL)
            {
                frfree(p_value);
                return APM_L7_ANALYZE_FAIL;
            }
            frfree(p_value);
            break;

        default:
            return APM_L7_ANALYZE_FAIL;
    }

    return APM_L7_ANALYZE_SUCC;
}

/*!
 * resp body数据解析
 *
 * \param p_packet        (resp数据)
 * \param p_st_private    (私有数据)
 * \param p_st_body_data
 *
 * \return APM_L7_ANALYZE_SUCC   成功\n
 *         APM_L7_ANALYZE_FAIL   失败
 *
 * \note
 *
 */
FR_STATIC int __resp_body_parse(Frbyte_Packet *p_packet
                        , L7session_dubbo_private *p_st_private
                        , Frreport_body_data *p_st_body_data)
{
    int i_ret = 0;
    char *p_value = NULL;

    if (p_packet == NULL || p_st_private == NULL || p_st_body_data == NULL)
        return APM_L7_ANALYZE_FAIL;

    switch(p_st_private->i_flag_serialize)
    {
        case 1://dubbo序列化
            i_ret = serialize_dubbo_readobject(p_packet, p_st_body_data, (char *)"response.rpc.return");
            if (i_ret == FR_FAIL)
                return i_ret;
            break;

        case 2://hessian2序列化
            i_ret = serialize_hessian2_readobject(p_packet, p_st_private, p_st_body_data, (char *)"response.rpc.return");
            if (i_ret == FR_FAIL)
                return i_ret;
            break;

        case 5://json序列化
            p_value = (char *)frmalloc(p_packet->ui_all_len-17+1);
            if (p_value == NULL)
                return APM_L7_ANALYZE_FAIL;
            memset(p_value, 0x00, p_packet->ui_all_len-17+1);
            if (bytebuff_get_bytes(p_packet, p_value, p_packet->ui_all_len-17) == FR_FAIL)
            {
                frfree(p_value);
                return APM_L7_ANALYZE_FAIL;
            }
            if (frreport_body_l7_add_str(p_st_body_data, "response.rpc.return", p_value) == FR_FAIL)
            {
                frfree(p_value);
                return APM_L7_ANALYZE_FAIL;
            }
            frfree(p_value);
            break;

        default:
            return APM_L7_ANALYZE_FAIL;
    }

    return APM_L7_ANALYZE_SUCC;
}

/*!
 * req数据解析
 *
 * \param p_packet        (req数据)
 * \param p_st_private    (私有数据)
 * \param p_st_body_data
 * \param p_st_conn       (入力链接信息)
 *
 * \return APM_L7_ANALYZE_SUCC   成功\n
 *         APM_L7_ANALYZE_FAIL   失败
 *
 * \note
 *
 */
FR_STATIC int __req_parse(Frbyte_Packet *p_packet
                        , L7session_dubbo_private *p_st_private
                        , Frreport_body_data *p_st_body_data
                        , Conntrack *p_st_conn)
{
    int i_ret = 0;

    if (p_packet == NULL || p_st_private == NULL || p_st_body_data == NULL || p_st_conn == NULL)
        return APM_L7_ANALYZE_FAIL;

    i_ret = __req_header_parse(p_packet, p_st_private, p_st_body_data);
    if (i_ret == APM_L7_ANALYZE_FAIL)
        return i_ret;

    i_ret = __req_body_parse(p_packet, p_st_private, p_st_body_data);
    if (i_ret == APM_L7_ANALYZE_FAIL)
        return i_ret;


    return APM_L7_ANALYZE_SET_REQ(APM_L7_ANALYZE_SUCC);
}

/*!
 * resp数据解析
 *
 * \param p_packet   (resp数据)
 * \param p_st_private    (私有数据)
 * \param p_st_body_data
 * \param p_st_conn       (入力链接信息)
 *
 * \return APM_L7_ANALYZE_SUCC   成功\n
 *         APM_L7_ANALYZE_FAIL   失败
 *
 * \note
 *
 */
FR_STATIC int __resp_parse(Frbyte_Packet *p_packet
                         , L7session_dubbo_private *p_st_private
                         , Frreport_body_data *p_st_body_data
                         , Conntrack *p_st_conn)
{
    int i_ret = 0;
    uint8_t u8_len = 0;

    if (p_packet == NULL || p_st_private == NULL || p_st_body_data == NULL || p_st_conn == NULL)
        return APM_L7_ANALYZE_FAIL;

    i_ret = __resp_header_parse(p_packet, p_st_private, p_st_body_data);
    if (i_ret == APM_L7_ANALYZE_FAIL)
        return i_ret;

    if (p_st_private->i_flag_status == 1)
    {
        if (bytebuff_get_char(p_packet, &u8_len) == FR_FAIL)
            return APM_L7_ANALYZE_FAIL;
        //0x91和0x92是hessian2序列化，0x1a和0x1b是dubbo序列化，0x31和0x32是json序列化
        if (u8_len == 0x91 || u8_len == 0x92 || u8_len == 0x1a || u8_len == 0x1b || u8_len == 0x31 || u8_len == 0x32)
        {
            if (frreport_body_l7_add_int(p_st_body_data, "response.code", 0) == FR_FAIL)
                return APM_L7_ANALYZE_FAIL;
        }
        else
        {
            if (frreport_body_l7_add_int(p_st_body_data, "response.code", 1) == FR_FAIL)
                return APM_L7_ANALYZE_FAIL;
        }
    }
    else
    {
        if (frreport_body_l7_add_int(p_st_body_data, "response.code", 1) == FR_FAIL)
            return APM_L7_ANALYZE_FAIL;
    }

    i_ret = __resp_body_parse(p_packet, p_st_private, p_st_body_data);
    if (i_ret == APM_L7_ANALYZE_FAIL)
        return i_ret;

    return APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC);
}

/*!
 * 检查是否超时
 *
 * \param p_node
 * \param p_data
 *
 * \return int
 *
 * \note
 *
 */
FR_STATIC int __check_timeout(Frlist_node* p_node, const void *p_data)
{
    dubbo_report_list *p_st_info = NULL;
    Frhash_header *p_header = (Frhash_header *)p_data;

    p_st_info = (dubbo_report_list *)p_node->data;

    if (p_st_info->st_request_time.tv_sec + SIP_TIMEOUT <= g_st_time.tv_sec)
    {
        frhash2_del(p_header, p_node, __list_node_free);
        return 1;
    }

    return 1;
}

/*!
 * 清除hash中超时的node
 *
 * \param p_hash_stmt
 * \param st_timestamp
 *
 * \return int
 *
 * \note
 *
 */
FR_STATIC void __clear_timeout_node(Frhash_header *p_header, struct timeval st_timestamp)
{
    g_st_time = st_timestamp;
    frhash2_run(p_header, p_header, __check_timeout);

    return;
}

/*!
 * dubbo的packet解析
 *
 * \param p_st_conn          (入力链接信息)
 * \param p_st_packet       (入力packet信息)
 * \param p_st_hash_report   (保存Frreport_data)
 * \param p_st_session       (私有数据)
 *
 * \return APM_L7_ANALYZE_SUCC   成功\n
 *         APM_L7_ANALYZE_FAIL   失败
 *         APM_L7_ANALYZE_TODO
 *
 * \note
 *
 */
FR_STATIC int __req_data_analysis(Conntrack *p_st_conn
                                , Apm_packet *p_st_packet
                                , Frhash_header *p_st_hash_report
                                , L7_session *p_st_session)
{
    uint32_t u32_len = 0;
    int i_ret = 0;
    unsigned long ul_tmp = 0;
    L4_transaction *p_st_l4_trans = NULL;
    Frreport_data* p_st_report_data = NULL;
    L7session_dubbo_private *p_st_private = NULL;

    if (p_st_conn == NULL || p_st_packet == NULL
        || p_st_hash_report == NULL || p_st_session == NULL)
        return APM_L7_ANALYZE_FAIL;

    p_st_private = (L7session_dubbo_private*)p_st_session->p_session_private;
    p_st_l4_trans = &(p_st_private->st_l4_trans);

    //创建数据链表
    if (p_st_private->p_st_body_data_req == NULL)
    {
        p_st_private->p_st_body_data_req = frreport_body_new_l7(p_st_conn);
        if (p_st_private->p_st_body_data_req == NULL)
            goto ERROR;

        if (frreport_body_l7_add_connInfo(p_st_private->p_st_body_data_req, p_st_conn) == FR_FAIL)
            goto ERROR;

        if (p_st_l4_trans->st_request_time_first_byte.tv_sec==0 && p_st_l4_trans->st_request_time_first_byte.tv_usec==0)
        {
            //st_request_time_first_byte
            p_st_l4_trans->st_request_time_first_byte.tv_sec = p_st_packet->st_timestamp.tv_sec;
            p_st_l4_trans->st_request_time_first_byte.tv_usec = p_st_packet->st_timestamp.tv_usec;
        }

        //创建bytebuff
        if (p_st_private->p_req_packet == NULL)
        {
            p_st_private->p_req_packet = bytebuff_creat();
            if (p_st_private->p_req_packet == NULL)
                goto ERROR;
        }
    }
    //请求的字节数
    p_st_l4_trans->l_request_bytes += p_st_packet->ui_payload_len;

    if (p_st_private->p_req_packet == NULL)
        goto ERROR;
    //把这个包的数据先全加进bytebuff里
    if (bytebuff_add(p_st_private->p_req_packet, p_st_packet->p_payload, p_st_packet->ui_payload_len) != FR_SUCC)
        goto ERROR;

    if (bytebuff_skip(p_st_private->p_req_packet, 12) != FR_SUCC)
        goto ERROR;
    if (bytebuff_get_int(p_st_private->p_req_packet, 4, &u32_len, BYTE_BIG_ENDIAN) == FR_FAIL)
        goto ERROR;
    if (u32_len+16 > p_st_private->p_req_packet->ui_all_len)
    {
        bytebuff_rewind(p_st_private->p_req_packet);
        return APM_L7_ANALYZE_TODO;
    }
    bytebuff_rewind(p_st_private->p_req_packet);

    //req解析
    i_ret = __req_parse(p_st_private->p_req_packet, p_st_private, p_st_private->p_st_body_data_req, p_st_conn);
    if (i_ret == APM_L7_ANALYZE_FAIL)
        goto ERROR;

    //数据上报
    if (frreport_body_l7_add_ulong(p_st_private->p_st_body_data_req, APM_REPORT_KEY_REQ_ID, frct_seq_increase(p_st_conn)) == FR_FAIL)
        goto ERROR;

    //start time
    if (frreport_body_l7_add_timeval(p_st_private->p_st_body_data_req, APM_REPORT_KEY_REQ_START_TIMESTAMP, &(p_st_l4_trans->st_request_time_first_byte)) == FR_FAIL)
        goto ERROR;

    //st_request_time
    p_st_l4_trans->st_request_time.tv_sec = p_st_packet->st_timestamp.tv_sec;
    p_st_l4_trans->st_request_time.tv_usec = p_st_packet->st_timestamp.tv_usec;
    ul_tmp = frtime_diff(&(p_st_l4_trans->st_request_time_first_byte), &(p_st_l4_trans->st_request_time));
    //send_time_us
    if (frreport_body_l7_add_long(p_st_private->p_st_body_data_req, APM_REPORT_KEY_REQ_SEND_TIME_US, ul_tmp) == FR_FAIL)
        goto ERROR;

    //request bytes
    if (frreport_body_l7_add_long(p_st_private->p_st_body_data_req, APM_REPORT_KEY_REQ_DATA_BYTES, p_st_l4_trans->l_request_bytes) == FR_FAIL)
        goto ERROR;

    //end time
    if (frreport_body_l7_add_timeval(p_st_private->p_st_body_data_req, APM_REPORT_KEY_REQ_END_TIMESTAMP, &p_st_l4_trans->st_request_time) == FR_FAIL)
        goto ERROR;

    p_st_report_data = frreport_data_l7_new(FRREPORT_TYPE_L7DATA_REQ);
    if (p_st_report_data == NULL)
        goto ERROR;

    //p_lst_req p_lst_comm添加到链表
    if (p_st_private->i_flag_oneway == 0)
    {
        i_ret = __dubbo_add_req_list(p_st_private);
        if (i_ret == FR_FAIL)
            goto ERROR;
    }

    __clear_timeout_node(p_st_private->p_report_list_dubbo, p_st_packet->st_timestamp);

    if (frreport_data_add_body(p_st_report_data, p_st_private->p_st_body_data_req) == FR_FAIL)
        goto ERROR;
    p_st_private->p_st_body_data_req = NULL;

    if (frreport_data_hash(p_st_hash_report, p_st_report_data) == FR_FAIL)
        goto ERROR;
    p_st_report_data = NULL;

    dubbo_session_reset(p_st_private);

    return APM_L7_ANALYZE_SET_REQ(APM_L7_ANALYZE_SUCC);

ERROR:
    if (p_st_report_data)
        frreport_data_free(&p_st_report_data);
    dubbo_session_reset(p_st_private);
    return APM_L7_ANALYZE_FAIL;
}

/*!
 * dubbo的packet解析
 *
 * \param p_st_conn          (入力链接信息)
 * \param p_st_packet        (入力packet信息)
 * \param p_st_hash_report   (保存Frreport_data)
 * \param p_st_session       (私有数据)
 *
 * \return APM_L7_ANALYZE_SUCC   成功\n
 *         APM_L7_ANALYZE_FAIL   失败
 *         APM_L7_ANALYZE_TODO
 *
 * \note
 *
 */
FR_STATIC int __resp_data_analysis(Conntrack *p_st_conn
                                 , Apm_packet *p_st_packet
                                 , Frhash_header *p_st_hash_report
                                 , L7_session *p_st_session)
{
    uint32_t u32_len = 0;
    int i_ret = 0;
    unsigned long ul_tmp = 0;
    L7session_dubbo_private *p_st_private = NULL;
    L4_transaction *p_st_l4_trans = NULL;
    Frreport_data* p_st_report_data = NULL;
    Frlist_node *p_st_gotnode = NULL;
    struct timeval st_request_time;

    if (p_st_conn == NULL || p_st_packet == NULL
        || p_st_hash_report == NULL || p_st_session == NULL)
        return APM_L7_ANALYZE_FAIL;

    p_st_private = (L7session_dubbo_private*)p_st_session->p_session_private;
    p_st_l4_trans = &(p_st_private->st_l4_trans);

    if (p_st_private->p_st_body_data_resp == NULL)
    {
        //创建数据链表
        p_st_private->p_st_body_data_resp = frreport_body_new_l7(p_st_conn);
        if (p_st_private->p_st_body_data_resp == NULL)
            goto ERROR;

        if (p_st_l4_trans->st_response_time_first_byte.tv_sec == 0 && p_st_l4_trans->st_response_time_first_byte.tv_usec == 0)
        {
            p_st_l4_trans->st_response_time_first_byte.tv_sec = p_st_packet->st_timestamp.tv_sec;
            p_st_l4_trans->st_response_time_first_byte.tv_usec = p_st_packet->st_timestamp.tv_usec;
        }

        //创建bytebuff
        if (p_st_private->p_resp_packet == NULL)
        {
            p_st_private->p_resp_packet = bytebuff_creat();
            if (p_st_private->p_resp_packet == NULL)
                goto ERROR;
        }
    }

    //请求的字节数
    p_st_l4_trans->l_response_bytes += p_st_packet->ui_payload_len;

    if (p_st_private->p_resp_packet == NULL)
        goto ERROR;
    //把这个包的数据先全加进bytebuff里
    if (bytebuff_add(p_st_private->p_resp_packet, p_st_packet->p_payload, p_st_packet->ui_payload_len) != FR_SUCC)
        goto ERROR;

    if (bytebuff_skip(p_st_private->p_resp_packet, 12) != FR_SUCC)
        goto ERROR;
    if (bytebuff_get_int(p_st_private->p_resp_packet, 4, &u32_len, BYTE_BIG_ENDIAN) == FR_FAIL)
        goto ERROR;
    if (u32_len+16 > p_st_private->p_resp_packet->ui_all_len)
    {
        bytebuff_rewind(p_st_private->p_resp_packet);
        return APM_L7_ANALYZE_TODO;
    }
    bytebuff_rewind(p_st_private->p_resp_packet);

    //resp解析
    i_ret = __resp_parse(p_st_private->p_resp_packet, p_st_private, p_st_private->p_st_body_data_resp, p_st_conn);
    if (i_ret == APM_L7_ANALYZE_FAIL)
        goto ERROR;

    //取链表对应的request p_lst_req p_lst_comm
    p_st_gotnode = frhash2_getLong(p_st_private->p_report_list_dubbo, p_st_private->u64_resp_id);
    if (p_st_gotnode == NULL)
        goto ERROR;
    //数据上报
    FRREPORT_BODY_L7LIST_SET_REQ(p_st_private->p_st_body_data_resp, &(((dubbo_report_list *)(p_st_gotnode->data))->p_lst_req));
    FRREPORT_BODY_L7LIST_SET_COMM(p_st_private->p_st_body_data_resp, &(((dubbo_report_list *)(p_st_gotnode->data))->p_lst_comm));
    ((dubbo_report_list *)(p_st_gotnode->data))->p_lst_req = NULL;
    ((dubbo_report_list *)(p_st_gotnode->data))->p_lst_comm = NULL;

    //start time
    if (frreport_body_l7_add_timeval(p_st_private->p_st_body_data_resp, APM_REPORT_KEY_RESP_START_TIMESTAMP, &(p_st_l4_trans->st_response_time_first_byte)) == FR_FAIL)
        goto ERROR;

    st_request_time.tv_sec = ((dubbo_report_list *)(p_st_gotnode->data))->st_request_time.tv_sec;
    st_request_time.tv_usec = ((dubbo_report_list *)(p_st_gotnode->data))->st_request_time.tv_usec;

    frhash2_del(p_st_private->p_report_list_dubbo, p_st_gotnode, __list_node_free);

    //server time
    if (st_request_time.tv_sec != 0 || st_request_time.tv_usec != 0)
    {
        ul_tmp = frtime_diff(&(st_request_time), &(p_st_l4_trans->st_response_time_first_byte));
        if (frreport_body_l7_add_ulong(p_st_private->p_st_body_data_resp, APM_REPORT_KEY_RESP_SERVER_TIME_US, ul_tmp) == FR_FAIL)
            goto ERROR;
    }
    else
    {
        if (frreport_body_l7_add_ulong(p_st_private->p_st_body_data_resp, APM_REPORT_KEY_RESP_SERVER_TIME_US, 0) == FR_FAIL)
            goto ERROR;
    }

    //st_response_time
    p_st_l4_trans->st_response_time.tv_sec = p_st_packet->st_timestamp.tv_sec;
    p_st_l4_trans->st_response_time.tv_usec = p_st_packet->st_timestamp.tv_usec;

    if (frreport_body_l7_add_long(p_st_private->p_st_body_data_resp, APM_REPORT_KEY_RESP_DATA_BYTES, p_st_l4_trans->l_response_bytes) == FR_FAIL)
        goto ERROR;

    //down load time
    ul_tmp = frtime_diff(&(p_st_l4_trans->st_response_time_first_byte), &(p_st_l4_trans->st_response_time));
    if (frreport_body_l7_add_long(p_st_private->p_st_body_data_resp, APM_REPORT_KEY_RESP_DOWNLOAD_TIME_US, ul_tmp) == FR_FAIL)
        goto ERROR;

    //end time
    if (frreport_body_l7_add_timeval(p_st_private->p_st_body_data_resp, APM_REPORT_KEY_RESP_END_TIMESTAMP, &(p_st_l4_trans->st_response_time)) == FR_FAIL)
        goto ERROR;

    p_st_report_data = frreport_data_l7_new(FRREPORT_TYPE_L7DATA_RESP);
    if (p_st_report_data == NULL)
        goto ERROR;

    if (frreport_data_add_body(p_st_report_data, p_st_private->p_st_body_data_resp) == FR_FAIL)
        goto ERROR;
    p_st_private->p_st_body_data_resp = NULL;

    if (frreport_data_hash(p_st_hash_report, p_st_report_data) == FR_FAIL)
        goto ERROR;
    p_st_report_data = NULL;

    dubbo_session_reset(p_st_private);

    return APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC);

ERROR:
    if (p_st_report_data)
        frreport_data_free(&p_st_report_data);
    dubbo_session_reset(p_st_private);
    return APM_L7_ANALYZE_FAIL;
}

/*!
 * dubbo的packet解析
 *
 * \param p_st_conn          (入力链接信息)
 * \param p_st_packet        (入力packet信息)
 * \param p_st_hash_report   (保存Frreport_data)
 *
 * \return APM_L7_ANALYZE_SUCC   成功\n
 *         APM_L7_ANALYZE_FAIL   失败
 *
 * \note
 *
 */
int dubbo_analysis(Conntrack *p_st_conn
                           , Apm_packet *p_st_packet
                           , Frhash_header *p_st_hash_report)
{
    int i_ret = APM_L7_ANALYZE_SUCC;
    L7_session *p_st_session = NULL;

    if (p_st_conn == NULL || p_st_packet == NULL
        || p_st_hash_report == NULL)
        return APM_L7_ANALYZE_FAIL;

    //到Conntrack中取得session信息
    p_st_session = frct_get_session(p_st_conn);
    if (p_st_session == NULL)
        return APM_L7_ANALYZE_FAIL;

    //fin、syn包则销毁私有数据
    if (p_st_packet->em_analyz_cmd == FRANALYZ_CMD_FIN || p_st_packet->em_analyz_cmd == FRANALYZ_CMD_SYN)
    {
        dubbo_session_destroy((void *)(p_st_session->p_session_private));
        p_st_session->p_session_private = NULL;
        return APM_L7_ANALYZE_SUCC;
    }

    //p_session_private分配内存
    if (p_st_session->p_session_private == NULL)
    {
        p_st_session->p_session_private = __dubbo_private_create();
        //内存分配失败
        if (p_st_session->p_session_private == NULL)
            return APM_L7_ANALYZE_FAIL;
    }

    //request
    if (p_st_packet->i_direction == FR_DIRECT_CLIENT2SRV)
    {
        i_ret = __req_data_analysis(p_st_conn,p_st_packet,p_st_hash_report,p_st_session);
        if (i_ret == APM_L7_ANALYZE_FAIL)
            goto ERROR;

        return i_ret;
    }
    //response
    else
    {
        i_ret = __resp_data_analysis(p_st_conn,p_st_packet,p_st_hash_report, p_st_session);
        if (i_ret == APM_L7_ANALYZE_FAIL)
            goto ERROR;

        return i_ret;
    }

ERROR:
    if (p_st_session->p_session_private)
        dubbo_session_destroy(p_st_session->p_session_private);
    p_st_session->p_session_private = NULL;
    return APM_L7_ANALYZE_FAIL;
}

/*!
 * 私有信息销毁
 *
 * \param p_private_session  (dubbo的私有session数据)
 *
 * \return FR_SUCC   成功\n
 *
 * \note
 *
 */
int dubbo_session_destroy(void *p_private_session)
{
    L7session_dubbo_private *p_st_private = (L7session_dubbo_private*)p_private_session;

    if (p_st_private)
    {
        if (p_st_private->p_st_body_data_req)
        {
            frreport_body_free(&(p_st_private->p_st_body_data_req));
            p_st_private->p_st_body_data_req = NULL;
        }

        if (p_st_private->p_st_body_data_resp)
        {
            frreport_body_free(&(p_st_private->p_st_body_data_resp));
            p_st_private->p_st_body_data_resp = NULL;
        }

        if (p_st_private->p_req_packet)
        {
            bytebuff_free(&(p_st_private->p_req_packet));
            p_st_private->p_req_packet = NULL;
        }

        if (p_st_private->p_resp_packet)
        {
            bytebuff_free(&(p_st_private->p_resp_packet));
            p_st_private->p_resp_packet = NULL;
        }

        if (p_st_private->p_fields_list_dubbo)
        {
            frlist_destroy(p_st_private->p_fields_list_dubbo, fields_list_node_free);
            p_st_private->p_fields_list_dubbo = NULL;
        }

        if (p_st_private->p_report_list_dubbo)
        {
            frhash2_free2(p_st_private->p_report_list_dubbo, __list_node_free);
            p_st_private->p_report_list_dubbo = NULL;
        }

        p_st_private->u64_req_id = 0;
        p_st_private->u64_resp_id = 0;
        p_st_private->i_flag_status = 0;
        p_st_private->i_flag_oneway = 0;
        p_st_private->i_flag_serialize = 0;
        p_st_private->i_len = 0;

        frfree(p_st_private);
        p_st_private = NULL;
    }

    return FR_SUCC;
}

/*!
 * 私有信息reset
 *
 * \param p_private_session  (dubbo的私有session数据)
 *
 * \return FR_SUCC   成功\n
 *
 * \note
 *
 */
void dubbo_session_reset(void *p_private_session)
{
    L7session_dubbo_private *p_st_private = NULL;

    if (p_private_session == NULL)
        return;

    p_st_private = (L7session_dubbo_private*)p_private_session;

    if (p_st_private)
    {
        memset(&(p_st_private->st_l4_trans), 0x00, sizeof(L4_transaction));

        if (p_st_private->p_st_body_data_req)
        {
            frreport_body_free(&p_st_private->p_st_body_data_req);
            p_st_private->p_st_body_data_req = NULL;
        }

        if (p_st_private->p_st_body_data_resp)
        {
            frreport_body_free(&p_st_private->p_st_body_data_resp);
            p_st_private->p_st_body_data_resp = NULL;
        }

        if (p_st_private->p_req_packet)
        {
            bytebuff_free(&(p_st_private->p_req_packet));
            p_st_private->p_req_packet = NULL;
        }

        if (p_st_private->p_resp_packet)
        {
            bytebuff_free(&(p_st_private->p_resp_packet));
            p_st_private->p_resp_packet = NULL;
        }

        if (p_st_private->p_fields_list_dubbo)
        {
            frlist_destroy(p_st_private->p_fields_list_dubbo, fields_list_node_free);
            p_st_private->p_fields_list_dubbo = NULL;
        }

        p_st_private->u64_req_id = 0;
        p_st_private->u64_resp_id = 0;
        p_st_private->i_flag_status = 0;
        p_st_private->i_flag_oneway = 0;
        p_st_private->i_flag_serialize = 0;
        p_st_private->i_len = 0;
    }

    return;
}
